import Vue from 'vue'
import Vuex from  'vuex'
import Storage from '../storage/storage'
Vue.use(Vuex);

const store=new Vuex.Store({
    state:{
        playSong:Storage.fetch('playSong'),                                   //当前播放的歌曲
        playList:Storage.fetch('playList'),            //当前播放的列表
        curPlayIndex:Storage.fetch('curPlayIndex'),                               //当前播放歌曲的索引
        showFooter:false,                              //是否显示底部播放器
        showDatail:false,                              //是否显示播放详情页 
        audio:null,                                    //audio的DOM节点
        isPlayed:false,                                //是否有音乐在播放
        modeType:'order',                              //播放模式
        paused:false,                                  //当前音乐是否暂停
        showPlayList:false,                            //是否显示播放列表
        showSearchResult:false,                       //是否显示搜索结果
        searchCurAnimate:-1                           //搜索结果当前动画索引
    },
    mutations:{
        setPlaySong(state,playSong){
            state.playSong=playSong;
        },
        setPlayList(state,playList){
            state.playList=playList;
        },
        setCurPlayIndex(state,curPlayIndex){
            state.curPlayIndex=curPlayIndex;
        },
        setShowFooter(state,showFooter){
            state.showFooter=showFooter;
        },
        setShowDatail(state,showDatail){
            state.showDatail=showDatail;
        },
        setAudio(state,audio){
            state.audio=audio;
        },
        setAudioSrc(state,audioSrc){
            state.audioSrc=audioSrc;
        },
        setIsPlayed(state,isPlayed){
            state.isPlayed=isPlayed;
        },
        setPaused(state,paused){
            if(paused ===true || paused===false){
                state.paused=paused;
                return;
            }
            if(!state.playSong.songmid) {
                state.paused = false;
                return;
            }
            state.paused = !state.paused;
            if(state.paused) {
                state.audio.pause();
            }
            else {
                state.audio.play();
            }
        },
        setModeType(state,modeType){
            state.modeType=modeType;
        },
        setShowPlayList(state,showPlayList){
            state.showPlayList=showPlayList;
        },
        setShowSearchResult(state,showSearchResult){
            state.showSearchResult=showSearchResult;
        },
        setSearchCurAnimate(state,searchCurAnimate){
            state.searchCurAnimate=searchCurAnimate;
        }
    },
    getters:{
        // 播放列表歌曲总数
        listTotal:state=>state.playList.length,
        //当前歌曲播放来源
        audioSrc:state=>{
            //检测是否空对象方法
            let isEmptyObject=(obj)=>{
                for(let key in obj){
                    return false;
                };
                return true;
            };
            if(!isEmptyObject(state.playSong)){
                return 'http://ws.stream.qqmusic.qq.com/C100'+state.playSong.songmid+'.m4a?fromtag=0&guid=126548448';
            }else{
                return '';
            }
        },
        // playCurTime:state=>{
        //     let isEmptyObject=(obj)=>{
        //         for(let key in obj){
        //             return false;
        //         };
        //         return true;
        //     };
        //     if(!isEmptyObject(state.audio)){
        //         return state.audio.currentTime;
        //     }else{
        //         return -1;
        //     }
        // }
    }
})

export default store